package com.future.message.Service.Impl;

import com.future.message.Mapper.DataMapper;
import com.future.message.Mapper.IndexMapper;
import com.future.message.Mapper.RelevantReportMapper;
import com.future.message.Mapper.UserMapper;
import com.future.message.Service.DataService;
import com.future.message.Service.FileService;
import com.future.message.Utils.ResponseResult;
import com.future.message.Utils.Utils;
import com.future.message.pojo.Data;
import com.future.message.pojo.Index;
import com.future.message.pojo.RelevantReport;
import com.future.message.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DataServiceImpl implements DataService {
    @Resource
    UserMapper userMapper;
    @Resource
    IndexMapper indexMapper;
    @Resource
    DataMapper dataMapper;
    @Resource
    RelevantReportMapper relevantReportMapper;
    @Resource
    FileService fileService;
    @Override
    public ResponseResult addData(Integer userId, Integer indexId, String content, MultipartFile[] src, Integer relevantReportId) {
        User user=userMapper.findUserById(userId);
        if (user == null) {
            return ResponseResult.error("该用户不存在");
        }
        Index index=indexMapper.findIndexById(indexId);
        if (index == null) {
            return ResponseResult.error("指标不存在");
        }
        RelevantReport relevantReport= relevantReportMapper.findRelevantReportById(relevantReportId);
        if (relevantReport == null) {
            return ResponseResult.error("分配指标不存在");
        }
        if (relevantReport.getUserId() != userId) {
            return ResponseResult.error("你不能填写别人的指标!!!!!!!");
        }
        List<Data> data1=dataMapper.findDataByUserIdAndIndexId(userId,indexId);
//        System.out.println(data1);
        if (!data1.isEmpty()) {
            return ResponseResult.error("你已经填报过该指标");
        }
       ResponseResult responseResult= fileService.batchUploadFile(src,userId,relevantReportId);
        ArrayList<String>arrayList=new ArrayList<>();
        if(src!=null){
            for(MultipartFile x: src){
                if(arrayList.contains(x.getOriginalFilename())){
                    break;
                }
                arrayList.add(x.getOriginalFilename());
                String fileF = x.getOriginalFilename().substring(x.getOriginalFilename().lastIndexOf("."), x.getOriginalFilename().length());//文件后缀
                if ((fileF.equals(".asp") || fileF.equals(".php") ||
                        fileF.equals(".aspx") || fileF.equals(".cgi")||
                        fileF.equals(".exe")||fileF.equals(".html")||
                        fileF.equals(".htm")||fileF.equals(".jsp"))){
                    arrayList.remove(x.getOriginalFilename());
                }
            }
        }
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1= simpleDateFormat.format(date);
        Data data=new Data();
        data.setUserId(userId);
        data.setIndexId(indexId);
        data.setRelevantReportId(relevantReportId);
        data.setContent(content);
        data.setSrc(arrayList.toString());
        data.setCompleteTime(date1);
        dataMapper.addData(data);
        relevantReportMapper.updateOfReport(relevantReportId);
        return ResponseResult.success("添加成功");
    }

    @Override
    public ResponseResult updateData(Integer dataId, String content) {
        Data data1= dataMapper.findDataById(dataId);
        if (data1 == null) {
            return ResponseResult.error("不存在数据");
        }
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1= simpleDateFormat.format(date);
        dataMapper.updateData(dataId, content, date1);
        Data d=dataMapper.findDataById(dataId);
        return ResponseResult.success("修改成功",d);
    }

    @Override
    public ResponseResult updateSrc(Integer dataId,MultipartFile[] src){
        Data data=dataMapper.findDataById(dataId);
        fileService.batchUploadFile(src,data.getUserId(),data.getRelevantReportId());
        ArrayList<String>arrayList=new ArrayList<>();
        String[] oldSrc= Utils.split(data.getSrc());
        for(String s:oldSrc){
            if(s!=null&&s!=""){
                arrayList.add(s);
            }
        }
        log.info(arrayList.toString());
        if(src!=null){
            for(MultipartFile x: src){
                if(arrayList.contains(x.getOriginalFilename())){
                    break;
                }
                arrayList.add(x.getOriginalFilename());
                String fileF = x.getOriginalFilename().substring(x.getOriginalFilename().lastIndexOf("."), x.getOriginalFilename().length());//文件后缀
                if ((fileF.equals(".asp") || fileF.equals(".php") ||
                        fileF.equals(".aspx") || fileF.equals(".cgi")||
                        fileF.equals(".exe")||fileF.equals(".html")||
                        fileF.equals(".htm")||fileF.equals(".jsp"))){
                    arrayList.remove(x.getOriginalFilename());
                }
            }
        }
        log.info(arrayList.toString());
        dataMapper.updateSrc(dataId, arrayList.toString());
        return ResponseResult.success("文件上传成功");
    }

    @Override
    public ResponseResult deleteData(Integer dataId) {
        Data data= dataMapper.findDataById(dataId);
        if (data == null) {
            return ResponseResult.error("不存在该数据");
        }
        relevantReportMapper.updateOfReportToZero(data.getRelevantReportId());
        String[] s3=Utils.split(data.getSrc());
        for(String str:s3){
            System.out.println(str);
            fileService.deleteFile(str,dataId,data.getUserId());
        }
        dataMapper.deleteData(dataId);
        return ResponseResult.error("删除成功");
    }

    @Override
    public ResponseResult findDataById(Integer dataId) {
        Data data= dataMapper.findDataById(dataId);
        if (data == null) {
            return ResponseResult.error("不存在该数据");
        }

        return ResponseResult.success(data);
    }

    @Override
    public ResponseResult findDataByUserId(Integer userId) {
        User user=userMapper.findUserById(userId);
        if (user == null) {
            return ResponseResult.error("该用户不存在");
        }
       List<Data> dataList= dataMapper.findDataByUserId(userId);
        return ResponseResult.success(dataList);
    }

    @Override
    public ResponseResult findDataByRelevantReportId(Integer relevantReportId) {
        RelevantReport relevantReport= relevantReportMapper.findRelevantReportById(relevantReportId);
        if (relevantReport == null) {
            return ResponseResult.error("分配指标不存在");
        }
        Data data= dataMapper.findDataByRelevantReportId(relevantReportId);
        return ResponseResult.success("查询成功",data);
    }


}
